package com.csx.governance.traffic.route.config;

import com.csx.governance.traffic.condition.ComponentFactory;
import com.csx.governance.traffic.core.interceptor.inbound.TrafficAbstractInboundHandlerInterceptor;
import com.csx.governance.traffic.core.support.TrafficMessageParameterExtractorRunner;
import com.csx.governance.traffic.route.Filter;
import com.csx.governance.traffic.route.swimlane.*;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author cuisongxu
 * @date 2023/2/22 19:33
 */

@Configuration
@EnableConfigurationProperties(value = {SwimLaneRulesConfiguration.class, SwimLaneDefinitionsConfiguration.class})
public class TrafficRouteAutoConfiguration {

    @Bean
    public SwimLaneGovernanceRuleLocator swimLaneGovernanceRuleLocator(SwimLaneRulesConfiguration swimLaneRulesConfiguration,
                                                                                           SwimLaneDefinitionsConfiguration swimLaneDefinitionsConfiguration,
                                                                                           ComponentFactory componentFactory) {
        return new SwimLaneGovernanceRuleLocator(swimLaneRulesConfiguration, swimLaneDefinitionsConfiguration, componentFactory);
    }

    @Bean
    public TrafficAbstractInboundHandlerInterceptor swimLaneResolverInboundHandlerInterceptor(Environment environment,
                                                                                              SwimLaneGovernanceRuleLocator swimLaneGovernanceRuleLocator,
                                                                                              TrafficMessageParameterExtractorRunner trafficMessageParameterExtractorRunner) {
        String localApplicationName = environment.getProperty("spring.application.name");
        return new SwimLaneResolverInboundHandlerInterceptor(localApplicationName, swimLaneGovernanceRuleLocator, trafficMessageParameterExtractorRunner);
    }

    @Bean
    public Filter swimLaneFilter(SwimLaneGovernanceRuleLocator swimLaneGovernanceRuleLocator) {
        return new SwimLaneFilter(swimLaneGovernanceRuleLocator);
    }

}
